package org.testis.gui.mvc.controller.testdesign;

import org.testis.gui.mvc.controller.*;
import org.testis.gui.mvc.model.testdesign.ProfileSettingsModel;
import org.testis.gui.mvc.utilities.AtofTableModel;
import org.testis.gui.mvc.view.testdesign.ProfileSettingsView;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.testis.orm.classes.T_test_profiles;
import org.testis.utils.middleware.GlobalDataHandler;


/**
 *
 * @author nurhat
 */
public class ProfileSettingsController extends AbstractController
{

    public static final String SHOW_VIEW = "ViewVisibility";
    public static final String PARAMETER_MODEL_CHANGED = "Model";
    public static final String SHOW_ERROR = "LastError";


    public ProfileSettingsController()
    {
        super();
        TestDesignControllerList.newInstance().setProfileSettingsController(this);

    }


    public void operationCreateCopy(int profileId)
    {
        GlobalDataHandler handler = new GlobalDataHandler();
       
        int newProfileId = handler.createCopyOfProfile(profileId);
        if (newProfileId > 0) {
            operationLoadProfiles();
        }
        else {
            setModelProperty(SHOW_ERROR,
                             "Unable to copy record...");
        }
    }


    public void startLoader()
    {
        initialize();
        setModelProperty(SHOW_VIEW,
                         true);

    }


    public void operationCloseWindow()
    {
        this.cleanAll();
    }


    public void initialize()
    {
        AtofTableModel temp = new AtofTableModel();
        temp.addColumn("Id");
        temp.addColumn("Name");
        ProfileSettingsModel model = new ProfileSettingsModel(temp);
        ProfileSettingsView view = new ProfileSettingsView(TestDesignControllerList.newInstance().
                                                           getParentFrame(),
                                                           true,
                                                           temp);
        super.addModel(model);
        super.addView(view);
        view.pack();
    }


    public HashMap operationGetProfiles()
    {
        GlobalDataHandler handler = new GlobalDataHandler();
        HashMap hmap = handler.getProfilesAsMap();
        return hmap;
    }


    public void operationLoadProfiles()
    {
        GlobalDataHandler handler = new GlobalDataHandler();
        HashMap hmap = handler.getProfilesAsMap();
        AtofTableModel temp = new AtofTableModel();
        temp.addColumn("Id");
        temp.addColumn("Name");
        Iterator iter = hmap.entrySet().iterator();
        while (iter.hasNext()) {
            Map.Entry entry = (Map.Entry) iter.next();
            String[] keyVal = new String[2];
            keyVal[0] = entry.getKey().toString();
            keyVal[1] = entry.getValue().toString();
            temp.addRow(keyVal);

        }
        setModelProperty(PARAMETER_MODEL_CHANGED,
                         temp);
    }


    public void operationAddProfile(String name)
    {
        GlobalDataHandler handler = new GlobalDataHandler();
        boolean result = handler.saveProfile(0,
                                             name);
        if (result) {
            operationLoadProfiles();
        }
        else {
            setModelProperty(SHOW_ERROR,
                             "Unable to add record...");
        }
    }


    public void operationEditProfile(int id,
                                     String name)
    {
        GlobalDataHandler handler = new GlobalDataHandler();
        boolean result = handler.saveProfile(id,
                                             name);
        if (result) {
            operationLoadProfiles();
        }
        else {
            setModelProperty(SHOW_ERROR,
                             "Unable to edit record...");
        }
    }


    public void operationRemoveProfile(int id)
    {
        GlobalDataHandler handler = new GlobalDataHandler();
        boolean result = handler.removeProfile(id);
        if (result) {
            operationLoadProfiles();
        }
        else {
            setModelProperty(SHOW_ERROR,
                             "Unable to remove record...");
        }
    }
}
